package philbot;

import java.util.HashMap;
import java.util.Map;
/**
 * a table of best responses
 * @author philip
 *
 */
public class PVTable {
	/**
	 * Entries in the table
	 * @author philip
	 *
	 */
	private static class Entry {
		int depth;
		AbstractPosition next;
		Entry(int depth, AbstractPosition next){
			this.depth = depth;
			this.next = next;
		}
	}
	private Map<AbstractPosition, Entry> map;

	/**
	 * Constructs a new empty table
	 */
	public PVTable(){
		map = new HashMap<AbstractPosition, Entry>();
	}
	
	/**
	 * Sets a refutation in the table.  If the position responded to already has a value attached, it is replaced iff the associated depth parameter is higher than used in the table
	 * @param depth the depth this position has been evaluated to
	 * @param from the current position
	 * @param to the best next position found
	 */
	public void setPV(int depth, AbstractPosition from, AbstractPosition to){
		if((to == null)||(from == null)){
			return;
		}
		Entry old = map.get(from);
		if(old == null){
			map.put(from, new Entry(depth,to));
		}else if(old.depth <= depth){
			map.put(from, new Entry(depth,to));
		}
	}
	
	/**
	 * @param the position to refute
	 * @return the best next move know, or NULL if no move is known
	 */
	public AbstractPosition getPV(AbstractPosition from){
		Entry v = map.get(from);
		if(v == null)
			return null;
		return v.next;
	}
	
}
